package accs.model.zalando;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import accs.model.AccountChecker;
import accs.model.Context;
import accs.model.ExtendedHttpClient;
import accs.model.utils.HeaderUtils;
import accs.model.utils.HtmlFormInputCollector;

public class ZalandoChecker implements AccountChecker {

	
	public ZalandoChecker() {
	}
	
	public ZalandoAccount login(String mail, String password, Context ctx) throws ClientProtocolException, IOException{
		DefaultHttpClient httpclient=new ExtendedHttpClient(ctx);
		
		HttpGet get=new HttpGet("https://www.zalando.de/login/");
		get.setHeader(HeaderUtils.getUserAgent());
		HttpResponse resp=httpclient.execute(get);
		
		List<String> whitelist=new ArrayList<String>(Arrays.asList(new String[]{"__fp","_sourcePage"}));
		List<NameValuePair> list=HtmlFormInputCollector.collectInputs(new InputStreamReader(resp.getEntity().getContent(),"utf-8"),whitelist);
//		for(NameValuePair bla : list) {
//			System.out.println(bla.getName()+" -> "+bla.getValue());
//		}
		
		HttpPost post=new HttpPost("https://www.zalando.de/login/");
		post.setHeader(HeaderUtils.getUserAgent());
		post.setHeader(HeaderUtils.getReferer("https://www.zalando.de/login"));
		
		list.add(new BasicNameValuePair("login","Anmelden"));
		list.add(new BasicNameValuePair("email",mail));
		list.add(new BasicNameValuePair("password",password));
		post.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
		
		HttpResponse resp2=httpclient.execute(post);
//		String location=resp2.getFirstHeader("Location").getValue();
		EntityUtils.consume(resp2.getEntity());
//		System.out.println(EntityUtils.toString(resp2.getEntity()));
//     	System.out.println("---------------------------------------------------------------");
		
		HttpGet get2=new HttpGet("https://www.zalando.de/benutzerkonto/zahlungsarten/");
		get2.setHeader(HeaderUtils.getUserAgent());
		get2.setHeader(HeaderUtils.getReferer("https://www.zalando.de/benutzerkonto"));
		HttpResponse resp3=httpclient.execute(get2);
//		EntityUtils.consume(resp3.getEntity());
//		System.out.println(EntityUtils.toString(resp3.getEntity()));
		
		BufferedReader br=new BufferedReader(new InputStreamReader(resp3.getEntity().getContent(),"utf-8"));
		String line=null;
	
		String paymentMethod = "N/A";
		int pay = -6;
		int linenumber = 0;
		boolean valid = true;
		while((line=br.readLine())!=null) {
			line=line.trim(); //System.out.println(line);
			linenumber++;
			
			if(line.contains("Ich bin bereits Zalando-Kunde")){
//				System.out.println(line);
				valid = false;
			}
			
			if(line.startsWith("<div class=\"box01\">")){
//				System.out.println(line);
				pay = linenumber+3;
			}
			if(linenumber == pay){
				paymentMethod = line.replace("<strong>", "").replace("</strong>", "");
			}
			
		}
		
		HttpGet get3=new HttpGet("https://www.zalando.de/benutzerkonto/bestellungen/");
		get3.setHeader(HeaderUtils.getUserAgent());
		get3.setHeader(HeaderUtils.getReferer("ttps://www.zalando.de/benutzerkonto/zahlungsarten/"));
		HttpResponse resp4=httpclient.execute(get3);
//		EntityUtils.consume(resp4.getEntity());
//		System.out.println(EntityUtils.toString(resp4.getEntity()));
		
		BufferedReader br2=new BufferedReader(new InputStreamReader(resp4.getEntity().getContent(),"utf-8"));
		String line2=null;
		
		int countOrders = 0;
		int linenumber2 = 0;
		while((line2=br2.readLine())!=null) {
			line2=line2.trim(); //System.out.println(line);
			linenumber2++;
			
			if(line2.contains("Bestellung ansehen")){
//				System.out.println(line);
				countOrders += 1;
			}
			
			
			if(line2.contains("Ich bin bereits Zalando-Kunde")){
//				System.out.println(line);
				valid = false;
			}
			
			
		}
		
		if(valid){
			ZalandoAccount za = new ZalandoAccount(mail, password, countOrders , paymentMethod);
			return za;
		}else{
			return null;
		}
	}


	public URL getImage() {
		return ZalandoChecker.class.getResource("/accs/gui/img/zalando.png");
	}

	public String getName() {
		return "zalando";
	}
}